package com.example.myhttp2;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    Button button1,button2,button3;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1=findViewById(R.id.button1);
        button2=findViewById(R.id.button2);
        button3=findViewById(R.id.button3);
        imageView=findViewById(R.id.imageView);
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                testGet1();

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                testPost();

            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                testpng();
            }
        });

    }
    @SuppressLint("NewApi")
    public void testGet1(){
        //创建OkHttpClient实例对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建Request对象
        Request request = new Request.Builder()
                .url("https://www.httpbin.org/get?id=111")
                .addHeader("key","value")
                .get()
                .build();
        //执行Request请求
        //异步请求
    okHttpClient.newCall(request).enqueue(new Callback() {

        public void onFailure(Call call, IOException e) {
            //请求失败
        }

        public void onResponse(Call call, Response response) throws IOException {
            //请求成功
            Log.d("TestOkHttp",response.body().string());
        }
    });
        //同步请求
        /*
        try {
            Response response = okHttpClient.newCall(request).execute();
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    @SuppressLint("NewApi")
    public void testPost(){
        //1、创建OkHttpClient对象实例
        OkHttpClient okHttpClient = new OkHttpClient();
        //2、创建Request对象
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(mediaType,"{}");
        Request request = new Request.Builder()
                .url("https://www.httpbin.org/post")
                .post(requestBody)
                .build();
        //3、执行Request请求
        okHttpClient.newCall(request).enqueue(new Callback() {

            public void onFailure(Call call, IOException e) {
                //请求失败
            }

            public void onResponse(Call call, Response response) throws IOException {
                //请求成功
                Log.d("TestOkHttpPost",response.body().string());
            }
        });

    }
    @SuppressLint("NewApi")
    public void testpng(){
        //1、创建OkHttpClient对象实例
        OkHttpClient okHttpClient = new OkHttpClient();
        //2、创建Request对象
        MediaType mediaType = MediaType.parse("image/png; charset=utf-8");
        RequestBody requestBody = RequestBody.create(mediaType,"{}");
        Request request = new Request.Builder()
                .url("https://www.httpbin.org/image/png")
                .get()
                .build();
        //3、执行Request请求
        okHttpClient.newCall(request).enqueue(new Callback() {

            public void onFailure(Call call, IOException e) {
                //请求失败
            }

            public void onResponse(Call call, final Response response) throws IOException {

                final MediaType contenttype = response.body().contentType();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Drawable drawable = Drawable.createFromStream(response.body().byteStream(),"image.png");
                        drawable.setBounds(0,0,1000,1000);
                        imageView.setImageDrawable(drawable);
                    }
                });


                //请求成功

            }
        });

    }

}